package mr.zhang.dao.impl;

import mr.zhang.dao.ProjectManagerDao;
import mr.zhang.pojo.model.TCensusProject;
import mr.zhang.pojo.model.TProject;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jerry on 15/2/5.
 * E-mail:huaxinxc@me.com
 */
@Repository("projectManagerDao")
public class ProjectManagerImpl implements ProjectManagerDao {

    private static Logger logger = LoggerFactory.getLogger(ProjectManagerImpl.class);

    @Autowired
    private SessionFactory sessionFactory;



    /**
     * 取得全部项目组的数据
     * 本数据由数据库 存储过程中完成
     *
     * @return
     */
    @Override

    public List<TCensusProject> getPojectList() {
        Session session = sessionFactory.openSession();

        List<TCensusProject> list = new ArrayList<TCensusProject>();
        list = session.createQuery("FROM TCensusProject").list();

        session.close();
        return list;
    }

    /**
     * 插入项目
     *
     * @param tProject
     * @return
     */

    @Override
    public Boolean addProject(TProject tProject) {
        Session session = sessionFactory.openSession();
        try {

            Transaction tx =  session.beginTransaction();
            session.save(tProject);
            tx.commit();

        }catch (Exception e){
            e.printStackTrace();
            logger.error("error" + e);
        }

        logger.info(" 执行添加操作 ");
        session.close();

        return true;
    }
}
